package org.ceteca.explica.client.gui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.explica.core.extTables.ExtFieldOT;
import org.ceteca.explica.core.extTables.ExtTableOT;
import org.ceteca.javaglade.Gui;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;



/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class ExplicaExtensionFieldsView extends GenericHandler {

	private Tree extCatalog;
	private Logger logger;
	private CommonClient entityLauncher;
	private ExtTableOT extTable;

	public ExplicaExtensionFieldsView(Gui parentGui) {
		super();
		this.setGui(parentGui);
	}

	public Tree getExtCatalog() {
		return extCatalog;
	}

	public void setExtCatalog(Tree extCatalog) {
		this.extCatalog = extCatalog;
	}

	public Logger getLogger() {
		return logger;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	
	
	
	/**
	 * 	 Remove the contents of the table and create the columns if necesary
	 */
	public void clear() {

		if (extCatalog == null)
			return;

		extCatalog.setHeaderVisible(true);

		// Delete the previous contents on the catalog tree
		extCatalog.clearAll(true);
		extCatalog.removeAll();

		// Add the tree item editor
		EntityExtensionCatalogListener listener = new EntityExtensionCatalogListener(
				extCatalog);
		extCatalog.addListener(SWT.Selection, listener);

		if (extCatalog.getColumnCount() == 0) {

			int columnWidth = 230;
			TreeColumn column = new TreeColumn(extCatalog, SWT.LEFT);
			column
					.setText(ClientContext
							.getInstance()
							.getI18n()
							.tr(
									InterfaceExplica.EXPLICA_ENTITY_EXTENSION_CATALOG_ID_COLUMN));
			column.setWidth(columnWidth);
			column.setMoveable(false);
			column.setResizable(true);
			column = new TreeColumn(extCatalog, SWT.LEFT);
			column
					.setText(ClientContext
							.getInstance()
							.getI18n()
							.tr(
									InterfaceExplica.EXPLICA_ENTITY_EXTENSION_CATALOG_VALUE_COLUMN));
			column.setWidth(columnWidth);
			column.setMoveable(false);
			column.setResizable(true);
			column = new TreeColumn(extCatalog, SWT.LEFT);
			column
					.setText(ClientContext
							.getInstance()
							.getI18n()
							.tr(
									InterfaceExplica.EXPLICA_ENTITY_EXTENSION_CATALOG_TYPE_COLUMN));
			column.setWidth(columnWidth);
			column.setMoveable(false);
			column.setResizable(true);
		}

	}

	public void setExtTable(ExtTableOT extT) {
		clear();
		this.extTable = extT;
		this.logger.debug("Setting extension Table " + extT.getCodExtTable());
	}



	public ExtTableOT getExtTable() {
		return extTable;
	}

	public CommonClient getEntityLauncher() {
		return entityLauncher;
	}

	public void setEntityLauncher(CommonClient entityLauncher) {
		this.entityLauncher = entityLauncher;
	}

	
	
	
	
	/**
	 * @return
	 * ArrayList<ExtFieldOT> List of fields stored on the three.
	 */
	public ArrayList<ExtFieldOT> getExtensionFields() {
		ArrayList<ExtFieldOT> lExtFields = new ArrayList<ExtFieldOT>();
		for (TreeItem item: extCatalog.getItems()){
				ExtFieldOT extField = (ExtFieldOT) item.getData();
				lExtFields.add(extField);
		}
		return lExtFields;
	}
	
	
	
	
	/**
	 * Updates the fields stored on the tree with the values displayed
	 */
	public void  updateExtensionFields() {
		for (TreeItem item: extCatalog.getItems()){
				ExtFieldOT extField = (ExtFieldOT) item.getData();
				extField.updateValue(item.getText(1));
		}
	}
	
	
	
	
	
	/**Fills the tree and displays it
	 * @param m
	 * Map<String, ExtFieldOT> Set of values to be displayed
	 * 
	 */
	public void fillExtendedFieldsTree(Map<String, ExtFieldOT> m){
		
		//clear();
		if (extTable != null && extTable.getFieldValues() != null) {
			Iterator<?> it = extTable.getFieldValues().keySet().iterator();
			int i = 0;
			
			while (it != null && it.hasNext()) {
				String codExtField = (String) it.next();
				ExtFieldOT extField = (ExtFieldOT) extTable
						.getFieldValues().get(codExtField);
				
				ExtFieldOT extFieldVal = m != null ? m
						.get(codExtField) : null;
						
				String[] lValues = new String[] {
						extField.getFieldDescription(),
						extFieldVal != null ? extFieldVal
								.getValueAsString() : "",
						ClientContext.getInstance().getI18n().tr(
								extField.getFieldTypeAsString()) 
								};
				TreeItem treeItem = (TreeItem) ExplicaGUIBuilder
						.getInstance()
						.getToolkit()
						.treeItem(
								extCatalog,
								InterfaceExplica.EXPLICA_ENTITY_EXTENSION_TREEITEM_NAME
										+ "_" + codExtField, lValues, null,
								i);
				treeItem.setData(extField);
				this.gui.addWidget(InterfaceExplica.EXPLICA_ENTITY_EXTENSION_TREEITEM_NAME + "_" + codExtField, 
						treeItem,
						InterfaceExplica.EXPLICA_ENTITY_EXTENSION_CATALOG_NAME
						);

				i++;
			}
		}
	}		

}
